package health.lm.com.mine.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.blankj.utilcode.util.StringUtils;
import com.jakewharton.rxbinding2.view.RxView;
import com.lm.sjy.titlebar.widget.CommonTitleBar;
import com.vondear.rxtool.RxSPTool;

import java.io.File;

import butterknife.BindView;
import butterknife.ButterKnife;
import health.lm.com.component_base.base.mvp.activity.XActivity;
import health.lm.com.component_base.helper.ImageLoaderHelper;
import health.lm.com.component_base.widget.CircleImageView.CircleImageView;
import health.lm.com.component_base.widget.SelectAvatarAlertDialog;
import health.lm.com.data.App;
import health.lm.com.data.AppModel;
import health.lm.com.mine.R;
import health.lm.com.mine.R2;
import health.lm.com.mine.entity.SettingBean;
import health.lm.com.mine.pop.circle.ChangeInfoServicePopUtil;
import health.lm.com.mine.present.SettingPresenter;
import health.lm.com.router.ARouterConstant;

/**
 * 设置页面
 */
@Route(path = ARouterConstant.SettingActivity, extras = ARouterConstant.NEED_LOGIN)
public class SettingActivity extends XActivity<SettingPresenter> implements SelectAvatarAlertDialog.SelectAvatarListener {

    @BindView(R2.id.title_bar)
    CommonTitleBar titlebar;
    @BindView(R2.id.iv_setting_head)
    CircleImageView ivSettingHead;
    @BindView(R2.id.ll_setting_head)
    LinearLayout llSettingHead;
    @BindView(R2.id.tv_setting_name)
    TextView tvSettingName;
    @BindView(R2.id.ll_setting_name)
    LinearLayout llSettingName;
    @BindView(R2.id.ll_change_psw)
    LinearLayout llChangePsw;
    @BindView(R2.id.tv_login_out)
    TextView tvLoginOut;
    @BindView(R2.id.tv_setting_mobile)
    TextView tvSettingMobile;
    @BindView(R2.id.ll_setting_mobile)
    LinearLayout llSettingMobile;
    @BindView(R2.id.tv_setting_mail)
    TextView tvSettingMail;
    @BindView(R2.id.ll_setting_mail)
    LinearLayout llSettingMail;
    @BindView(R2.id.ll_wallet_psw)
    LinearLayout llWalletPsw;
    @BindView(R2.id.tv_setting_organization)
    TextView tvSettingOrganization;
    @BindView(R2.id.ll_setting_organization)
    LinearLayout llSettingOrganization;
    @BindView(R2.id.tv_setting_duty)
    TextView tvSettingDuty;
    @BindView(R2.id.ll_setting_duty)
    LinearLayout llSettingDuty;
    @BindView(R2.id.tv_edit_introduce)
    TextView tvEditIntroduce;
    @BindView(R2.id.tv_setting_introduce)
    TextView tvSettingIntroduce;
    @BindView(R2.id.tv_edit_service)
    TextView tvEditService;
    @BindView(R2.id.tv_setting_service)
    TextView tvSettingService;

    private SelectAvatarAlertDialog avatarAlertDialog;

    @Override
    public int getLayoutId() {
        return R.layout.activity_setting;
    }

    @Override
    public SettingPresenter newP() {
        return new SettingPresenter();
    }

    @SuppressLint("CheckResult")
    @Override
    public void initData(Bundle savedInstanceState) {
        avatarAlertDialog = new SelectAvatarAlertDialog(context);
        avatarAlertDialog.setSelectAvatarListener(this);
        //返回按钮
        titlebar.setListener((v, action, extra) -> finish());
        //设置头像
        RxView.clicks(llSettingHead)
                .compose(bindToLife())
                .subscribe(o -> avatarAlertDialog.show());
        //修改名字
        RxView.clicks(llSettingName)
                .compose(bindToLife())
                .subscribe(o -> withValueActivity(ARouterConstant.SettingNameActivity)
                        .withString(ARouterConstant.USER_NAME, tvSettingName.getText().toString())
                        .navigation(context, ARouterConstant.SUCCESS));
        //修改手机号
        RxView.clicks(llSettingMobile)
                .compose(bindToLife())
                .subscribe(o -> withValueActivity(ARouterConstant.SettingMobileActivity)
                        .withString(ARouterConstant.USER_NAME, tvSettingName.getText().toString())
                        .navigation(context, ARouterConstant.SUCCESS));
        //企业邮箱
        RxView.clicks( llSettingMail)
                .compose(bindToLife())
                .subscribe(o -> withValueActivity(ARouterConstant.SettingMailActivity)
                        .withString(ARouterConstant.USER_NAME, tvSettingMail.getText().toString())
                        .navigation(context, ARouterConstant.SUCCESS));
        //修改密码
        RxView.clicks(llChangePsw)
                .compose(bindToLife())
                .subscribe(o -> gotoActivity(ARouterConstant.ChangePswActivity));
        //钱包密码
        RxView.clicks(llWalletPsw)
                .compose(bindToLife())
                .subscribe(o -> gotoActivity(ARouterConstant.SettingWalletPswActivity));
        //个人简介
        RxView.clicks(tvEditIntroduce)
                .compose(bindToLife())
                .subscribe(o -> ChangeInfoServicePopUtil.getInstance().init(this,3,tvEditIntroduce));
        //服务内容
        RxView.clicks(tvEditService)
                .compose(bindToLife())
                .subscribe(o -> ChangeInfoServicePopUtil.getInstance().init(this,4,tvEditIntroduce));
        //退出登录
        RxView.clicks(tvLoginOut)
                .compose(bindToLife())
                .subscribe(o -> getP().loginOut());
        getP().settingData();
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        avatarAlertDialog.onActivityResult(requestCode, resultCode, data);
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == ARouterConstant.SUCCESS) {
            if (data != null) {
                notifyData();
            }
        }
    }

    public void changeHeadSuccess(String url) {
        //用户头像
//        ImageLoaderHelper.getInstance().load(this, url, ivSettingHead);
        notifyData();
    }

    public void loginOutSuccess() {
        App.getModel().clear();
        ARouter.getInstance().build(ARouterConstant.LoginActivity).navigation();
        finish();
    }

    /**
     * 选择头像之后的返回值
     */
    @Override
    public void selectAvatarSuccess(File outputFile, Uri outputUri) {
        getP().uploadHead(outputFile);
    }

    public void getSettingData(SettingBean settingBean){
        //用户头像
        if(!StringUtils.isEmpty(settingBean.getAvatar())) {
            ImageLoaderHelper.getInstance().load(this, settingBean.getAvatar(), ivSettingHead);
        }
        //用户名
        tvSettingName.setText(settingBean.getReal_name());
        //电话
        tvSettingMobile.setText(settingBean.getMobile());
        //企业邮箱
        tvSettingMail.setText(settingBean.getEmail());
        //所属商会
        tvSettingOrganization.setText(settingBean.getBussiness_name());
        //职务
        tvSettingDuty.setText(settingBean.getDuties_name());
        //个人简介
        tvSettingIntroduce.setText(settingBean.getBz());
        //服务内容
        tvSettingService.setText(settingBean.getServer());

        RxSPTool.putString(context, AppModel.phone,settingBean.getMobile());
    }

    public void notifyData(){
        getP().settingData();
    }
}
